package com.ruoyi.job.actuator.impl;

import com.ruoyi.job.domain.SysJobVo;
import com.ruoyi.job.util.AbstractQuartzJob;
import com.ruoyi.job.util.CommandUtil;
import com.ruoyi.job.util.ScheduleUtils;
import com.ruoyi.job.vo.CommandResult;
import com.ruoyi.job.vo.TaskParam;
import org.quartz.JobExecutionContext;

import java.util.Random;

/**
 * Shell执行器
 *
 * @author liangli_lmj@126.com
 * @date 2024-11-28
 */
public class ShellExecution extends AbstractQuartzJob {

    @Override
    protected CommandResult doExecute(JobExecutionContext context, SysJobVo sysJobVo, TaskParam taskParam) throws Exception {
        String[] strArray = ScheduleUtils.commandParams(taskParam);
        Random random = new Random();
        // 生成50到100之间的随机整数（包含50，不包含100）
        int sleepSeconds = random.nextInt(300) + 500;

        System.out.println("线程将要睡眠 " + sleepSeconds + " 秒");

        // 将秒转换为毫秒，因为Thread.sleep()接受的是毫秒
        Thread.sleep(sleepSeconds * 1000);
        return CommandUtil.exec("sh", taskParam.getName(), strArray);
    }
}
